// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * Token2.h
 *
 *  Created on: Dec 11, 2009
 *      Author: dhovemey
 */

#ifndef TOKEN2_H_
#define TOKEN2_H_

#include <string>
#include "fe/Location.h"

/**
 * A source token.
 */
class Token2 {
public:
	/**
	 * Token type enumeration.
	 */
	enum TokenType {
		METHOD = 258,
		FIELD = 259,
		CLASS = 260,
		NEW = 261,
		SELF = 262,
		VAR = 263,
		WHILE = 264,
		IF = 265,
		ELSE = 266,
		IMPORT = 267,
		AS = 268,
		MODULE = 269,
		RETURN = 270,
		NIL = 271,
		MAGIC = 272,
		IDENT = 273,
		SYMBOL_LITERAL = 274,
		STRING_LITERAL = 275,
		INT_LITERAL = 276,
		TRUE = 277,
		FALSE = 278,
		DOT = 279,
		COMMA = 280,
		DOUBLECOLON = 281,
		COLON = 282,
		SEMICOLON = 283,
		LPAREN = 284,
		RPAREN = 285,
		LBRACE = 286,
		RBRACE = 287,
		NOT = 288,
		ASSIGN = 289,

		// binary operators: can be overridden as methods
		LOGICAL_OR = 290,
		LOGICAL_AND = 291,
		GREATER_THAN_EQ = 292,
		GREATER_THAN = 293,
		LESS_THAN_EQ = 294,
		LESS_THAN = 295,
		NOT_EQUALS = 296,
		EQUALS = 297,
		MINUS = 298,
		PLUS = 299,
		DIVIDE = 300,
		TIMES = 301,

		INVALID_TOKEN_TYPE = -1,
	};

	static const TokenType TOK_FIRST_BIN_OP = LOGICAL_OR;
	static const TokenType TOK_LAST_BIN_OP = TIMES;

	static bool isBinOp(TokenType tt);
	static bool isBinOpOrAssign(TokenType tt);
	static int getPrecedence(TokenType tt);
	static bool isLeftAssoc(TokenType tt);
	static bool isRightAssoc(TokenType tt);
	static std::wstring getBinOpName(TokenType tt);

private:
	TokenType m_tokenType;
	std::wstring m_lexeme;
	Location m_loc;

	Token2 *m_prev, *m_next;

public:
	/**
	 * Constructor.
	 *
	 * @param tokenType  token type of the token
	 * @param lexeme     the lexeme of the token
	 * @param loc        source location
	 * @param prev       the previous token from the source text
	 */
	Token2(TokenType tokenType, const std::wstring &lexeme, const Location &loc, Token2 *prev);

	/** Destructor. */
	~Token2();

	/** Get token type (using token numbers assigned by the generated parser). */
	TokenType getTokenType() const;

	/** Get the lexeme. */
	const std::wstring &getLexeme() const;

	/** Get the source location. */
	const Location &getLocation() const;

	/** Get previous token in source text (null if none). */
	Token2 *getPrev() { return m_prev; }

	/** Get next token in source text (null if none). */
	Token2 *getNext() { return m_next; }
};

#endif /* TOKEN2_H_ */
